<style>
#PasswdDiv{
	text-align: center;
}
#PasswdPage{
	padding: 24px;
	margin: 16vh 1vw;
	border-radius: 2px;
	background: #AAA;
	display: inline-block;
	box-shadow: 2px 2px 3px #343;
}
#PasswdTable td{
	padding: 12px;
}
#PasswdTable input{
	padding: 7px 4px;
}
#PasswdButton{
	width: 100%;
	box-sizing: border-box;
}
#TitleLogoDiv{
	color: #555;
	font-size: 1.6rem;
	font-weight: bold;
	margin-bottom: 16px;
}
#PasswdSendMailButton{
	float: right;
	background: none;
	box-shadow: none;
	padding-right: 1px;
	border: 1px solid rgba(0,0,0,0);
}
#PasswdSendMailButton:hover{
	color: #982;
}
#PasswdSendMailButton:active{
	color: #234;
}
</style>

<div id='PasswdDiv'>
	<div id='PasswdPage'>
		<div id='TitleLogoDiv'>用户密码重置</div>
		<table id='PasswdTable'>
			<tr><td><v-text id='PasswdMailText' title='关联邮箱' maxlength='128' size='22'></v-text></td></tr>
			<tr><td><v-text id='PasswdCheckCodeText' title='邮箱验证' maxlength='6' size='6'></v-text><v-button id='PasswdSendMailButton' title='发送验证邮件'></v-button></td></tr>
			<tr><td><v-text id='PasswdUserPasswdText' title='登录密码' type='password' maxlength='32' size='22'></v-text></td></tr>
			<tr><td><v-text id='PasswdCheckPasswdText' title='确认密码' type='password' maxlength='32' size='22'></v-text></td></tr>
			<tr><td><v-button title='提交修改' id='PasswdButton'></v-button></td></tr>
		</table>
	</div>
</div>

<script>
var curmail = null;

getVue('PasswdPage');

$('#PasswdSendMailButton').click(function(){
	var mail = $('#PasswdMailText').val();
	
	if (strlen(mail) == 0){
		$('#PasswdMailText').focus();
	}
	else if (checkEmail(mail)){
		getHttpResult("/sendmail", {flag: 'P', mail: mail}, function(data){
			if (data.code >= 0){
				var sec = 60;
				var txt = $('#PasswdMailText');
				var btn = $('#PasswdSendMailButton');
				
				$('#CheckCodeText').val('');
				txt.attr('disabled', true);
				btn.attr('disabled', true);
				btn.val(sec + '秒后可重试');
				curmail = mail;
				
				var timer = setInterval(function(){
					if (--sec <= 0){
						txt.removeAttr('disabled');
						btn.removeAttr('disabled');
						btn.val('重新发送邮件');
						clearInterval(timer);
					}
					else{
						txt.attr('disabled', true);
						btn.attr('disabled', true);
						btn.val(sec + '秒后可重试');
					}
					
				}, 1000);
							}
			else if (data.code == XG_NOTFOUND){
				showToast('该邮箱还未注册');
			}
			else{
				showToast("发送频繁(请稍后再试)");
			}
		});
	}
	else{
		showToast('邮箱地址格式错误');
	}
});

$('#PasswdButton').click(function(){
	var mail = $('#PasswdMailText').val();
	var code = $('#PasswdCheckCodeText').val();
	var password = $('#PasswdUserPasswdText').val();
	var checkpwd = $('#PasswdCheckPasswdText').val();

	if (strlen(mail) == 0){
		$('#PasswdMailText').focus();
	}
	else if (strlen(curmail) == 0){
		showToast('请验证关联邮箱');
	}
	else if (mail != curmail){
		showToast('请重新验证关联邮箱');
	}
	else if (strlen(code) == 0){
		$('#PasswdCheckCodeText').focus();
	}
	else if (strlen(password) == 0){
		$('#PasswdUserPasswdText').focus();
	}
	else if (strlen(checkpwd) == 0){
		$('#PasswdCheckPasswdText').focus();
	}
	else if (password != checkpwd){
		showToast('两次输入密码不一致');
	}
	else{
		var param = {};
		
		param["flag"] = 'P';;
		param["mail"] = mail;
		param["code"] = code;
		param["password"] = password;
		
		getHttpResult('/edituser', param, function(data){
			if (data.code == XG_NOTFOUND){
				showToast('该邮箱还未注册');
			}
			else if (data.code == XG_AUTHFAIL){
				showToast('请输入正确的邮箱验证码');
			}
			else if (data.code < 0){
				showToast('登录密码更新失败');
			}
			else{
				showToast('登录密码更新成功');

				setTimeout(function(){
					window.history.back();
				}, 1000);
			}
		});
	}
});
</script>